package lyuba;

import java.util.Properties;
import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Console;



/** 
 * Backs up a specified directory tree to an Amazon Glacier vault to vaults.  
 * Maintains a database of files, file modification status and the associated
 * Glacier archives in a database.  Also contains the capability to restore a 
 * directory tree specified by a glacier database and to remove files that are
 * no longer required from the Glacier vault.
 * 
 * @author Matthew W. Smith
 * 
 * 		--configpath	the following argument is the path to the configuration
 * 						file
 * 		--backupdir		the root of the file structure to be backed up
 * 						recursively.  Incompatible with clean and restore.
 * 		--backup 		perform a backup of the specified directory structure
 * 		--restore		download and restore files from Glacier
 * 		--clean 		remove outdated files from Glacier
 *		--menu			ignore the configuration file and configure
 *						interactively
 *		--quiet			do not confirm additions or deletions from the vaults 
 */

public class lyuba {
	
	//private enum appSwitches {
	 //	configpath,
	//	backup,
	//	restore,
	//	clean
	//}
	
	protected enum Mode {CLEAN, BACKUP, RESTORE};
	
	protected static Mode mode = Mode.BACKUP;
	
	protected static boolean quiet = false;
	
	protected static boolean menu = false;
	
	protected static Properties lyubaProperties; 
	
	protected static int init(String[] initOpts){ 
		try{
		
			File config = null;	
			
			FileInputStream in = null;
			
		if (initOpts.length == 0) return(1); // status 1 = no args.
		
		// TODO add display of help for the no arg case
		
		for (int i = 0; i < initOpts.length; i++){
								
			if (initOpts[i].startsWith("--")){
				
				if (initOpts[i].substring(2) == "configpath") {
					try {config = new File(initOpts[i++]);
						if (!config.canRead()) {
						System.err.println("Unable to read configuration file"
								+ config.getName());
						return (2);
						}
						else {
						in = new FileInputStream(config);
						lyubaProperties.load(in);
						}	
					}
					catch (IOException x) {
						System.err.format("IOException: %s%n", x);
						return (2);
					}
					finally {if (in!=null) in.close();}
				}
							
				if (initOpts[i].substring(2) == "quiet"){
					quiet = true;
					continue;
				}
				
				if (initOpts[i].substring(2) =="menu"){ 
					menu = true;
					continue;
				}
				
				if (initOpts[i].substring(2) == "clean"){
					mode = Mode.CLEAN;
					continue;
				}
				
				if (initOpts[i].substring(2) == "backup"){
					mode = Mode.BACKUP;
					continue;
				}
				
				if (initOpts[i].substring(2) == "restore"){
					mode = Mode.RESTORE;
					continue;
				}
				
				System.err.println("Invalid argument " + initOpts[i]);
		}
	}
		if (menu == true) {
		try {
			Console cons = System.console();	
		menu1: {
			while (true) {System.out.println("SELECT LYUBA MODE%n" + 
					"1) Backup%n" +
					"2) Restore%n" +
					"3) Clean%n");
				String ans = cons.readLine("SELECTION:", "");
				if (ans == "1") {
					mode = Mode.BACKUP;
					break menu;
				}
				else if (ans == "2") {
					mode = Mode.RESTORE;
					break menu;
				}
				else if (ans == "3") {
					mode = Mode.CLEAN;
					break menu;
				}
				else System.err.println("Invalid selection");
				}
			}
		}
		catch (IOException x) {
			System.err.println("IOException: " + x.getMessage());		
		}
	}
	
}
	public static void main(String[] args){
		try{
			int initStatus = init(args);
			}
		}
	}

